Calculate elevation for a point inside a terrain
Consider any random
2D point in a space, and user wants to calculate its elevation based on some
terrain surface. Project the point on to terrain surface to check whether it is
inside terrain boundary or its external to terrain surface. The below code
snippet shows how to calculate elevation of a 2D point which exists inside
terrain boundary.
internal void CalculateEleForPntInsideTerrain()
{
//Get active dgn model
Bentley.DgnPlatformNET.DgnModel dgnModel = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel();
//Create connection to dgn model
Bentley.CifNET.SDK.ConsensusConnection consensusConnection = new ConsensusConnection(dgnModel);
//Get active geometric model
Bentley.CifNET.GeometryModel.SDK.GeometricModel geometricModel = consensusConnection.GetActiveGeometricModel();
foreach (Bentley.CifNET.GeometryModel.SDK.TerrainSurface ts in geometricModel.TerrainSurfaces)
{
//Get terrain model from terrain surface
Bentley.TerrainModelNET.DTM dtm = ts.DTM;
//Point 2D
Bentley.GeometryNET.DPoint3d currentPoint = new DPoint3d(70774.916616, 1210021.6587936000433);
Bentley.TerrainModelNET.DTMDrapedPoint drappedPoint = null;
double pntElevation = 0.0;
//project 2D point on 3D terrain and get drapped point/projected point
drappedPoint = dtm.DrapePoint(currentPoint);
if (drappedPoint != null)
{
//Check points position, projected point is not within terrain boundry
if (drappedPoint.Code == Bentley.TerrainModelNET.DTMDrapedPointCode.External)
{
//Point is external to terrain
return;
}
else //projected pt is inside terrain boundry
{
//Get elevation for point
pntElevation = drappedPoint.Coordinates.Z;
}
}
}
}
UseDrapePoint() of DTM to project a 2D point on terrain surface, the projected points Z coordinate will be the elevation for 2D point.